
//4
//2
//0
//1,0
//1,1
//2,0,1
//输出
//0,1


const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let n;
let start;
let initArr = [];
rl.on("line", (line)=>{
    if(n === undefined) {
        n = Number(line.trim())
    } else if(start === undefined) {
        start = Number(line.trim())
    } else {
        let arr = line.trim().split(",").map(Number)
        arr.shift()
        initArr.push(arr)
        if(initArr.length === n) {
            getResult(n, start, initArr)
            
            // clear
            n = start = undefined;
            initArr.length = 0;
        }

    }

})

function getResult(n, start, initArr) {
    if(initArr[start].length === 0) {
        console.log(null)
        return
    }
    let visited = {}
    for (const i in initArr) {
        let arr = initArr[i]
        if(!visited[i]) {
            visited[i] = {
                visited: false,
                depend: []
            }
        }
        visited[i].depend = [...arr]
    }

    let result = [];
    let flag = true;
    dfs(start, visited)
    if(!flag) {
        console.log(-1)
        return
    }
    result = Array.from(new Set(result))
    console.log(result.sort((a, b) => a - b).join(","))

    function dfs(start, visited) {
        // end
        if(!flag) return
        if(visited[start].depend.length === 0) return;
        visited[start].visited = true;

        for (const child of visited[start].depend) {
            if(visited[child].visited === true) {
                flag = false;
                return;
            }
            result.push(child)
            dfs(child, visited)
        }
    }
}

